package com.zy.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/12 16:16
 **/
public class NIOTest {
    public static void main(String[] args) {
        File f1 = new File("d:/temp/test.txt");
        File f2 = new File("d:/temp/test-copy.txt");
        saveToFilesystem(f1,f2);
    }
    //NIO(New Intput /Output)
        public static void saveToFilesystem (File src, File dist){
            try {
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(dist);

                FileChannel inChannel = fis.getChannel();
                FileChannel outChannel = fos.getChannel();

                inChannel.transferTo(0, inChannel.size(), outChannel);
                inChannel.close();
                outChannel.close();
                fis.close();
                fos.close();
            } catch (Exception ex) {
                ex.printStackTrace() ;
            }
        }

}
